package ldh.im.fxgui.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaException;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import ldh.im.fxgui.Main;

import java.io.File;
import java.net.URI;
import java.util.function.Supplier;

public class AudioRecorderExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox vBox = new VBox();
        MediaView mediaView = new MediaView();

        Button button = new Button("play");
        button.setOnAction((e)->{
            try{
                String path = Main.class.getResource("/media/call.mp3").toExternalForm();
                MediaPlayer player = new MediaPlayer(new Media(path));
                player.play();
//                Thread.sleep(10000);
                String file1 = "F:\\BaiduNetdiskDownload\\廖彩杏 JY Books 1-7周音频(13CD)\\09 Green Eggs adn Ham\\Track05.mp3";
                String file2 = "E:\\test\\imFileDir\\upload\\20250110\\11892.m4a";
//        String file2 = "C:\\Users\\37835\\Desktop\\20250109114703568.m4a";
                File file = new File(file2);
                URI uri = file.toURI();
                System.out.println(uri);

                if (button.getUserData() != null && button.getUserData().toString().equals("playing")) return;
                button.setUserData("playing");

//                playAudio(mediaView, path, ()->{
//                    button.setUserData("end");
//                    return true;
//                });
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        });

        vBox.getChildren().addAll(button, mediaView);

        Scene scene = new Scene(vBox, 800, 600);

        scene.getStylesheets().add(this.getClass().getResource("/demo/texta.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    void playAudio(MediaView mediaView, String audioPath, Supplier<Boolean> supplier) {
        if (audioPath == null) return;
        Media media = new Media(audioPath);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);
        mediaPlayer.onEndOfMediaProperty().set(()->{
            supplier.get();
            mediaPlayer.dispose();
        });
        mediaPlayer.setAutoPlay(true);
    }

    public void play(MediaView  mediaView) throws InterruptedException {
        String path = "E:\\test\\imFileDir\\upload\\20250107\\11858.mp3";
        File file = new File(path);
        Media media = new Media(file.toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);
        mediaPlayer.currentTimeProperty().addListener((l)->{
            System.out.println("time:" + mediaPlayer.getCurrentTime());
        });
        mediaPlayer.statusProperty().addListener(((observableValue, status, t1) -> {
            System.out.println("status:" + mediaPlayer.getStatus());
        }));

        mediaPlayer.setVolume(1.0);
        mediaPlayer.play();

//        long time = (long) mediaPlayer.getTotalDuration().toMillis();
//        time = time < 6000 ? 6000 : time;
//        // 等待音频播放完毕
//        Thread.sleep(time);
//
//        mediaPlayer.dispose();
    }
}
